package com.forever7776.life.web.controller.api;

import com.alibaba.fastjson.JSONObject;
import com.forever7776.life.web.controller.BaseController;
import org.apache.commons.codec.binary.Hex;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

/**
 * <p>
 * sendcloud回调处理
 * </p>
 *
 * @author kz
 * @date 2018-03-12
 */
@Controller
@RequestMapping("/api/sendcloud/verify")
public class SendCloudVerifyController extends BaseController {

    @RequestMapping
    public String index(String appkey,
                        String token,
                        long timestamp,
                        String signature,
                        String message) throws NoSuchAlgorithmException, InvalidKeyException {
        logger.info("appkey:{}", appkey);
        logger.info("token:{}", token);
        logger.info("timestamp:{}", appkey);
        logger.info("signature:{}", signature);
        logger.info("message:{}", message);
        Mac sha256HMAC = Mac.getInstance("HmacSHA256");
        SecretKeySpec secretKey = new SecretKeySpec(appkey.getBytes(), "HmacSHA256");
        sha256HMAC.init(secretKey);
        StringBuffer buf = new StringBuffer();
        buf.append(timestamp).append(token);
        String signatureCal = new String(Hex.encodeHex(sha256HMAC.doFinal(buf
                .toString().getBytes())));
        logger.info("signature:{}", signature);
        JSONObject result = new JSONObject();
        result.put("status", 200);
        return result.toJSONString();
    }
}
